package com.developmentReformProject.controller;

import com.alibaba.fastjson.JSON;
import com.developmentReformProject.dao.FiveProjectDao;
import com.developmentReformProject.dao.ThreeProjectDao;
import com.developmentReformProject.dto.FiveQueryDto;
import com.developmentReformProject.dto.ThreeQueryDto;
import com.developmentReformProject.entity.FiveYearProject;
import com.developmentReformProject.entity.ThreeYearProject;
import com.developmentReformProject.service.ThreeYearProjectLibraryService;
import com.developmentReformProject.utils.FileUtil;
import com.developmentReformProject.utils.PageUtil;
import com.developmentReformProject.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/three")
public class ThreeController {
    @Autowired
    private ThreeProjectDao threeProjectDao;
    @Autowired
    private ThreeYearProjectLibraryService threeYearProjectLibraryService;
    @Autowired
    private FiveProjectDao fiveProjectDao;
    @GetMapping("/queryList")
    public ResultVo list(ThreeQueryDto threeQueryDto) throws Exception{
        //调用业务
        PageUtil<ThreeYearProject> data= threeYearProjectLibraryService.pageList(threeQueryDto);
        //响应
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @PostMapping("/delete")
    public ResultVo deleteEnterpriseAdmin(int threeProjectNo)throws Exception{
        threeProjectDao.deleteProject(threeProjectNo);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/update")
    public ResultVo updateEnterpriseAdmin(ThreeYearProject threeYearProject)throws Exception{
        threeProjectDao.updateThree(threeYearProject);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/addProject")
    public ResultVo addProject(ThreeYearProject threeYearProject,@RequestParam("file") MultipartFile file) throws Exception{
      //  String fileName= FileUtil.upload(file);//文件上传
      // threeYearProject.setThreeProjectAssignmentPaper(fileName);//把路径存入数据库
        threeProjectDao.addThree(threeYearProject);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @GetMapping("/download")
    public void download(int threeProjectNo, HttpServletResponse resp) throws Exception {
        ThreeYearProject threeYearProject=threeProjectDao.queryByNo(threeProjectNo);
        String filename=threeYearProject.getThreeProjectAssignmentPaper();
        FileUtil.download(filename, resp);//下载数据
    }
    @GetMapping("/listDelete")
    public ResultVo listDelete(ThreeQueryDto threeQueryDto) throws Exception {
        PageUtil<ThreeYearProject> data =threeYearProjectLibraryService.cleanThree(threeQueryDto);
        ResultVo resultVo=new ResultVo<>(data);
        return  resultVo;
    }
    @PostMapping("/reduction")
    public ResultVo reduction(int threeProjectNo) throws Exception{
        ThreeYearProject threeYearProject=new ThreeQueryDto();
        threeYearProject.setThreeProjectNo(threeProjectNo);
        threeYearProject.setThreeStateId(1);
        threeProjectDao.updateThree(threeYearProject);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/cleanThree")
    public ResultVo cleanFive(int threeProjectNo)throws Exception{
        threeProjectDao.cleanThree(threeProjectNo);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @GetMapping("/threeCount")
    public int threeCount()throws Exception{
        ThreeQueryDto threeQueryDto=new ThreeQueryDto();
        int mag=threeProjectDao.countSelective(threeQueryDto);
        return mag;
    }
    @GetMapping("/countSum")
    public Double countSum()throws Exception{
        Double sum=threeProjectDao.countSum()+fiveProjectDao.countSum();
        return Math.floor(sum/100000000);
    }
    @GetMapping("/countType")
    public String countType()throws Exception{
        return JSON.toJSONString(fiveProjectDao.countType());
    }
    @GetMapping("/count")
    public String count() {
        List<Integer> list=new ArrayList();
        list.add(threeProjectDao.countOne());
        list.add(threeProjectDao.countTwo());
        list.add(threeProjectDao.countThree());
        list.add(threeProjectDao.countFour());
        list.add(threeProjectDao.countFive());
        list.add(threeProjectDao.countSix());
        System.out.println(JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }
}
